<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增信用评价任务计划')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body>
<div class="main-content">
    <form id="form-task-add" class="form-horizontal">
        <input name="deptIds"  type="hidden" id="deptIds"/>
        <div class="row">
            <div class="col-sm-12">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        基础信息
                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-sm-6">
                                <div class="form-group">
                                    <label class="col-sm-3 control-label is-required">任务名称：</label>
                                    <div class="col-sm-8">
                                        <input name="taskName" class="form-control" type="text" required>
                                    </div>
                                </div>
                            </div>
                            <div class="col-sm-3">
                                <div class="form-group">
                                    <label class="col-sm-3 control-label is-required">收支类别：</label>
                                    <div class="col-sm-8">
                                        <select id="contractClass" name="contractClass" class="form-control m-b" th:with="type=${@dict.getType('contract_class')}" required>
                                            <option value="">所有</option>
                                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                        </select>
                                    </div>
                                </div>
                            </div>

                            <div class="col-sm-3">
                                <div class="form-group">
                                    <label class="col-sm-3 control-label is-required">合同类型：</label>
                                    <div class="col-sm-8">
                                        <select id="contractType" name="contractType" class="form-control m-b" th:with="type=${@dict.getType('contract_type')}" required>
                                            <option value="">所有</option>
                                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-sm-12">
                <div class="panel panel-info">
                    <div class="panel-heading">
                        合同信息
                    </div>
                    <div class="panel-body">
                        <button type="button" class="btn btn-white btn-sm" onclick="addContract()"><i
                                class="fa fa-search">
                            选择</i></button>
                        <button type="button" class="btn btn-white btn-sm" onclick="sub.delColumn()"><i
                                class="fa fa-minus">
                            删除</i></button>
                        <div class="col-sm-12 select-table table-striped table-bordered">
                            <table id="contract-table"></table>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-sm-12">
                <div class="panel panel-info">
                    <div class="panel-heading">
                        部门信息
                    </div>
                    <div class="panel-body">
                        <button type="button" class="btn btn-white btn-sm" onclick="selectDeptTree()"><i
                                class="fa fa-search">
                            选择</i></button>
                        <button type="button" class="btn btn-white btn-sm" onclick="delColumn()"><i
                                class="fa fa-minus">
                            删除</i></button>
                        <div class="col-sm-12 select-table table-striped table-bordered">
                            <table id="dept-table"></table>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </form>
</div>

<div class="row">
    <div class="col-sm-offset-5 col-sm-10">
        <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;
        <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>
    </div>
</div>

<th:block th:include="include :: footer" />
<th:block th:include="include :: datetimepicker-js" />
<script th:inline="javascript">
    var prefix = ctx + "credit/task"

    var prefix_contract = ctx + "credit/taskContract";

    var targetTypeDatas = [[${@dict.getType('contract_party_type')}]];

    $("#form-task-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            $.operate.save(prefix + "/add", $('#form-task-add').serialize());
        }
    }

    $("input[name='taskYear']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    // 合同信息列表
    var options = {
        id: "contract-table",
        pagination: false,
        showSearch: false,
        showRefresh: false,
        showToggle: false,
        showColumns: false,
        /*url: prefix + "/credit/taskContract/list",*/
        modalName: "合同信息",
        columns: [{
            checkbox: true
        },
            {
                field: 'index',
                align: 'center',
                title: "序号",
                formatter: function (value, row, index) {
                    var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                    var columnId = $.common.sprintf("<input type='hidden' value='%s'>", index, row.id);
                    return columnIndex + $.table.serialNumber(index) + columnId;
                }
            },
            {
                field: 'contractId',
                title: '合同id',
                cellStyle: function (row, index) {
                    var style = {};
                    style={css:{'display':'none'}};
                    return style;
                },
                formatter: function(value, row, index) {
                    var html = $.common.sprintf("<input class='form-control' type='text' readonly name='taskContractList[%s].contractId' value='%s'>", index, value);
                    return html;
                }
            },
            {
                field: 'signYear',
                title: '签订年份',
                visible: false,
                formatter: function(value, row, index) {
                    var html = $.common.sprintf("<input class='form-control' type='text' readonly name='taskContractList[%s].signYear' value='%s'>", index, value);
                    return html;
                }
            },
            {
                field: 'contractName',
                title: '合同名称',
                formatter: function(value, row, index) {
                    var html = $.common.sprintf("<input class='form-control' type='text' readonly name='taskContractList[%s].contractName' value='%s'>", index, value);
                    return html;
                }
            },
            {
                field: 'targetType',
                title: '合约方类型',
                formatter: function(value, row, index) {
                    if(value == 1){
                        var hide = $.common.sprintf("<input type='hidden' name='taskContractList[%s].targetType' value='%s'>", index, value);
                        var html = $.common.sprintf("<input class='form-control' type='text' readonly  value='%s'>", '供应商');
                        return hide + html;
                    } else if(value == 2){
                        var hide = $.common.sprintf("<input type='hidden' name='taskContractList[%s].targetType' value='%s'>", index, value);
                        var html = $.common.sprintf("<input class='form-control' type='text' readonly  value='%s'>", '客户');
                        return hide + html;
                    }

                }
            },
            {
                field: 'targetName',
                title: '合约方名称',
                formatter: function(value, row, index) {
                    var html = $.common.sprintf("<input class='form-control' type='text' readonly name='taskContractList[%s].targetName' value='%s'>", index, value);
                    return html;
                }
            },
            {
                title: '操作',
                align: 'center',
                formatter: function(value, row, index) {
                    var actions = [];
                    actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                    return actions.join('');
                }
            }]
    };
    $.table.init(options);
    $('#contract-table').children().children().children('th').eq(2).css("display","none");
    // 选择合同
    function addContract(){
        // 判断合同收支类型，决定查询供应合同还是销售合同数据
        var contractClass = $("#contractClass").val();
        var contractType = $("#contractType").val();

        // 判断是否选择收支类型和合同类型
        if(contractClass == null || contractClass == "undefined" || contractClass == '' || contractType == null || contractType == "undefined" || contractType == ''){
            $.modal.alertError("请先完善基本信息，选择合同收支类型和合同类型")
        } else {
            // 支出类合同
            if(contractClass == 1){
                var options = {
                    title: '合同选择',
                    width: "880",
                    url: "/credit/contractPay/selectContract",
                    callBack: doContractSubmitPay
                };
                $.modal.openOptions(options);
            }
            // 收入类合同
            if(contractClass == 2){
                var options = {
                    title: '合同选择',
                    width: "880",
                    url: "/credit/contractIncome/selectContract",
                    callBack: doContractSubmitIncome
                };
                $.modal.openOptions(options);
            }
        }
    }

    // 支出类合同选择回调
    function doContractSubmitPay(index, layero){
        // var rows = layero.find("iframe")[0].contentWindow.getSelections();

        var iframeWin = layero.find("iframe")[0].contentWindow;
        // console.log(iframeWin);
        var selectTemplate = JSON.stringify(iframeWin.$('#bootstrap-table').bootstrapTable('getSelections'));
        var obj = JSON.parse(selectTemplate);
        console.log(obj);
        console.log(obj.length);

        if (obj.length == 0) {
            $.modal.alertWarning("请至少选择一个合同");
            return;
        }

        // 先清空表格数据
        var count = $("#contract-table").bootstrapTable('getData').length;
        if(count>0){
            $("#contract-table").bootstrapTable('removeAll');
        }

        // 增加到合同信息表
        for(var i=0;i<obj.length;i++){
            var per = {
                contractName: obj[i].contractName,
                targetType: 1,
                targetName: obj[i].supplierNames,
                signYear: obj[i].signYear,
                contractId: obj[i].id
            }
            addColumn_contract(per);
        }

        $.modal.close(index);
    }

    // 收入类合同选择回调
    function doContractSubmitIncome(index, layero){

        var iframeWin = layero.find("iframe")[0].contentWindow;
        // console.log(iframeWin);
        var selectTemplate = JSON.stringify(iframeWin.$('#bootstrap-table').bootstrapTable('getSelections'));
        var obj = JSON.parse(selectTemplate);
        console.log(obj);

        if (obj.length == 0) {
            $.modal.alertWarning("请至少选择一个合同");
            return;
        }
        // 先清空表格数据
        var count = $("#contract-table").bootstrapTable('getData').length;
        if(count>0){
            $("#contract-table").bootstrapTable('removeAll');
        }

        // 增加到合同信息表
        for(var i=0;i<obj.length;i++){
            var per = {
                contractName: obj[i].contractIncomeName,
                targetType: 2,
                targetName: obj[i].customerNames,
                signYear: obj[i].signYear,
                contractId: obj[i].id
            }
            addColumn_contract(per);
        }

        $.modal.close(index);
    }

    // 合同信息列表新增某一行
    function addColumn_contract(obj) {
        var count = $("#contract-table").bootstrapTable('getData').length;
        // sub.editColumn();

        $("#contract-table").bootstrapTable('insertRow', {
            index: count,
            row: {
                index: $.table.serialNumber(count),
                contractName: obj.contractName,
                targetType: obj.targetType,
                targetName: obj.targetName,
                signYear: obj.signYear,
                contractId: obj.contractId
            }
        });
    }

    // 部门信息列表
    var options_dept = {
        id: "dept-table",
        pagination: false,
        showSearch: false,
        showRefresh: false,
        showToggle: false,
        showColumns: false,
        /*url: prefix + "/credit/taskDept/list",*/
        modalName: "任务和部门关联",
        columns: [{
            checkbox: true
        },
            {
                field: 'index',
                align: 'center',
                title: "序号",
                formatter: function (value, row, index) {
                    var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                    var columnId = $.common.sprintf("<input type='hidden' value='%s'>", index, row.id);
                    return columnIndex + $.table.serialNumber(index) + columnId;
                }
            },
            {
                field: 'deptId',
                title: '部门id',
                cellStyle: function (row, index) {
                    var style = {};
                    style={css:{'display':'none'}};
                    return style;
                },
                formatter: function(value, row, index) {
                    var html = $.common.sprintf("<input class='form-control' type='text' readonly name='taskDeptList[%s].deptId' value='%s'>", index, value);
                    return html;
                }
            },
            {
                field: 'deptName',
                title: '部门名称',
                formatter: function(value, row, index) {
                    var html = $.common.sprintf("<input class='form-control' type='text' readonly name='taskDeptList[%s].deptName' value='%s'>", index, value);
                    return html;
                }
            },
            {
                field: 'weight',
                title: '权重',
                formatter: function(value, row, index) {
                    var html = $.common.sprintf("<input class='form-control' type='number' min='0' max='100' name='taskDeptList[%s].weight' value='%s'>", index, value);
                    return html;
                }
            },
            {
                title: '操作',
                align: 'center',
                formatter: function(value, row, index) {
                    var actions = [];
                    actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="delColumn(\'' + row.index + '\')"><i class="fa fa-remove"></i>删除</a>');
                    return actions.join('');
                }
            }]
    };
    $.table.init(options_dept);
    $('#dept-table').children().children().children('th').eq(2).css("display","none");
    // 选择部门
    function selectDeptTree() {
        var deptIds = $("#deptIds").val();
        if (!deptIds) {
            var url = '/system/dept/selectDeptTree/checkBox';
        } else {
            var url = '/system/dept/selectDeptTree/checkBox/' + deptIds;
        }
        $.modal.open("选择部门", url);
    }

    // 弹框选择回显调用方法
    function selectDeptCall(deptIds,deptNames){
        // 先清空表格数据
        var count = $("#dept-table").bootstrapTable('getData').length;
        if(count>0){
            $("#dept-table").bootstrapTable('removeAll');
        }
        // console.log(deptIds);
        // console.log(deptNames);
        // 如果有逗号，代表选择多个，以逗号分开
        // 如果没有逗号，可能没选，可能选了一个

        var arrId = deptIds.split(',');
        var arrName = deptNames.split(',');

        for(var i=0;i<arrId.length;i++){
            var obj = {
                deptId: arrId[i],
                deptName: arrName[i]
            }
            addColumn(obj);
        }
    }

    // 部门配置列表新增某一行
    function addColumn(obj) {
        var count = $("#dept-table").bootstrapTable('getData').length;
        // sub.editColumn();

        $("#dept-table").bootstrapTable('insertRow', {
            index: count,
            row: {
                index: $.table.serialNumber(count),
                deptId: obj.deptId,
                deptName: obj.deptName,
                weight: ""
            }
        });
    }
</script>
</body>
</html>
